package wangjg.security.resource.factory.db;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wangjg.security.core.ResourceFactory;
import wangjg.security.resource.factory.db.entity.Resource;
import wangjg.security.resource.factory.db.service.ResourceService;

@Service
public class DbResourceFactory implements ResourceFactory {
	
	@Autowired
	private ResourceService service;
	
	private Set<Resource> resources;
	
	public Iterator<Resource> iterator() {
		if(resources==null){
			resources = new HashSet<Resource>();
			this.reload();
		}
		return this.resources.iterator();
	}

	private void reload(){
//		service = SpringContextUtil.getBean(ResourceService.class);
		List<Resource> list = service.findAll();
		this.resources.addAll(list);
	}

	public ResourceService getService() {
		return service;
	}

	public void setService(ResourceService service) {
		this.service = service;
	}
	
}
